package com.cn.esermis.utils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class TableCreator {

    private final JdbcTemplate jdbcTemplate;

    public TableCreator(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    @Resource
    private TableUtils tableUtils;
    /**
     * 动态创建分表（按月）
     * @param logicTableName 逻辑表名（如 order）
     * @param tableSuffix    表后缀（如 202310）
     */
    public void createTableIfAbsent(String logicTableName, String tableSuffix) {
        String actualTableName = logicTableName + "_" + tableSuffix;
        String sql = "CREATE TABLE IF NOT EXISTS " + actualTableName + " LIKE " + logicTableName + "_0";
        jdbcTemplate.execute(sql);
        tableUtils.refreshExistingTables();
    }
}
